cc.Class({
    extends: cc.Component,

    properties: {
        itemNode: cc.Node,
        iconSpts: [cc.SpriteFrame],

        lb_bar1: cc.Label,
        lb_bar2: cc.Label,
        lb_bar3: cc.Label,
        lb_bar4: cc.Label,

    },


    onLoad () {
        this.container = this.itemNode.parent;
        this.itemNode.parent = null;
        this.bikeFights = ['', '', '', '', ''];
        this.bikeStatus = [0, 0, 0, 0, 0];
        this.bikeMiss = [0 ,0 ,0, 0, 0];
        cc.Linker(URLS.MotorsHistory, {
            page: 1,
            pageSize: 7
        }).request(data => this.initData(data));
        this.initWords();
    },
    initWords: function(){
        this.lb_bar1.string = LangLocal.word25;
        this.lb_bar2.string = LangLocal.word26;
        this.lb_bar3.string = LangLocal.word27;
        this.lb_bar4.string = LangLocal.word28;
    },
    initData(data) {
        var infos = data[0].bikeInfos;
        var bikes = GLOBAL.gameState.bikes;
        for(var i in infos) {
            this.bikeMiss[infos[i].bikeId - 1] = infos[i].miss;
            this.bikeStatus[bikes[i].id - 1] = bikes[i].status;
        }
        for(var i = 0; i < data.length; i++) {
            var d = data[i].bikeInfos;
            for(var j = 0; j < d.length; j++) {
                var str = d[j].rank + '';
                if(i < data.length - 1) str += '-';
                this.bikeFights[d[j].bikeId - 1] += str;
            }
        }
        for(var i = 0; i < 5; i++) this.initItem(i);
    },

    initItem(order) {
        var item = cc.instantiate(this.itemNode);
        item.parent = this.container;
        item.find('motor_icon', cc.Sprite).spriteFrame = this.iconSpts[order];
        item.find('motor_name', cc.RichText).string = '<color=#ffffff><outline color=black width=2><b>00' + (order + 1) + '</b></outline></color>';
        item.find('motorState', cc.ProgressBar).progress = this.bikeStatus[order];
        item.find('txtRecent', cc.Label).string = this.bikeFights[order];
        item.find('txtMiss', cc.Label).string = this.bikeMiss[order];
        item.find('btnDetail').on('click', () => cc.Popup.show('MotorsPop', pop => pop.switchInfo(order)), this);
    }
});
